﻿using Coldairarrow.Business.IT;
using Coldairarrow.Entity.IT;
using Coldairarrow.IBusiness.DTO;
using Coldairarrow.Util;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Coldairarrow.IBusiness;
using Coldairarrow.Entity.DTO;
using Microsoft.AspNetCore.Authorization;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.IO;
using NPOI.XSSF.UserModel;
using NPOI.SS.Util;
using System.Linq;
using System.Linq.Expressions;

namespace Coldairarrow.Api.Controllers.IT
{
    [Route("/IT/[controller]/[action]")]
    public class IT_LocalMaterialController : BaseApiController
    {
        #region DI

        public IT_LocalMaterialController(IIT_LocalMaterialBusiness iT_LocalMaterialBus, IOperator op, IServiceProvider provider)
        {
            _iT_LocalMaterialBus = iT_LocalMaterialBus;
            _Op = op;
            _provider = provider;
        }

        IIT_LocalMaterialBusiness _iT_LocalMaterialBus { get; }
        IOperator _Op { get; }
        IServiceProvider _provider { get; }

        #endregion

        #region 获取

        /// <summary>
        /// 获取物料存放周期报表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<StorageCycleDTO>> GetStorageCycleDataList(PageInput input)
        {
            return await _iT_LocalMaterialBus.GetStorageCycleDataListAsync(input);
        }

        [HttpPost]
        public async Task<PageResult<IT_LocalMaterial>> GetDataList(IT_LocalMaterialPageInput input)
        {
            input.StorId = _Op.Property.DefaultStorageId;
            return await _iT_LocalMaterialBus.GetDataListAsync(input);
        }

        [HttpPost]
        public async Task<PageResult<IT_LocalMaterial>> GetDataListByMaterialId(PageInput<ConditionDTO> input)
        {
            input.Search.Condition = _Op.Property.DefaultStorageId;
            return await _iT_LocalMaterialBus.GetDataListByMaterialId(input);
        }
        [NoCheckJWT]
        [HttpPost]
        public async Task<PageResult<IT_LocalMaterialDTO>> GetDataListAsyncDtoList(PageInput<IT_LocalMaterialConditionDTO> input) {

            return await _iT_LocalMaterialBus.GetDataListAsyncDtoList(input);
        }

        /// <summary>
        /// 查看货位占用量信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<AmountOccupied>> GetAmountOccupiedList(PageInput<AmountOccupiedConditionDTO> input)
        {
            return await _iT_LocalMaterialBus.GetAmountOccupiedListAsync(input);
        }

        /// <summary>
        /// 大屏显示出入库监控
        /// </summary>
        /// <returns>出入库监控</returns>
        [HttpPost]
        [NoCheckJWT]
        public async Task<List<IT_LocalMaterialOutInStatusDTO>> GetOutInStatus(int Days = 0)
        {
            return await _iT_LocalMaterialBus.GetOutInStatus(Days);
        }
        [HttpPost]
        public async Task<IT_LocalMaterial> GetTheData(IdInputDTO input)
        {
            return await _iT_LocalMaterialBus.GetTheDataAsync(input.id);
        }


        [HttpPost]
        public async Task<IT_LocalMaterial> GetTheLocalMaterial(BusinessInfo businessInfo)
        {
            businessInfo.StorId = _Op.Property.DefaultStorageId;
            return await _iT_LocalMaterialBus.GetDataByBussiness(businessInfo);
        }

        [HttpPost]
        public async Task<List<IT_LocalMaterial>> GetQueryData(SelectQueryDTO search)
        {
            return await _iT_LocalMaterialBus.GetQueryData(search, _Op.Property.DefaultStorageId);
        }



        /// <summary>
        /// PDA扫码获取货区货架货位
        /// </summary>
        /// <param name="locationQrCode">货位二维码</param>
        /// <param name="materialId">物料ID</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<IT_LocalMaterialDTO>> GetPDADataListAsyncDtoList(string locationQrCode, string materialId)
        {

            return await _iT_LocalMaterialBus.GetPDADataListAsyncDtoList(locationQrCode, materialId);
        }

        [HttpPost]
        public async Task<List<IT_LocalMaterialDTO>> GetDataListAsynclist() {
            var res = await _iT_LocalMaterialBus.GetDataListAsynclist();
            return res;
        }

        /// <summary>
        ///导出报表接口  通用  朱庆龙     2022/3/23
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [NoCheckJWT]
        public async Task<IActionResult> GetExportList()
        {
            List<IT_LocalMaterialDTO> res = await _iT_LocalMaterialBus.GetDataListAsynclist();
            HSSFWorkbook book = new HSSFWorkbook();
            ISheet s1 = book.CreateSheet("库存报表");
            IRow r1 = s1.CreateRow(0);
            r1.CreateCell(0).SetCellValue("货区 /货架/货位");
            r1.CreateCell(1).SetCellValue("编码");
            r1.CreateCell(2).SetCellValue("物料");
            r1.CreateCell(3).SetCellValue("规格");
            r1.CreateCell(4).SetCellValue("仓库");
            r1.CreateCell(5).SetCellValue("单位");
            r1.CreateCell(6).SetCellValue("库存数量");
            r1.CreateCell(7).SetCellValue("单重");
            r1.CreateCell(8).SetCellValue("总重");
            r1.CreateCell(9).SetCellValue("可用数量");
            r1.CreateCell(10).SetCellValue("占用数量");
            for (int i = 0; i < res.Count; i++)
            {
                IRow rt = s1.CreateRow(i + 1);
                rt.CreateCell(0).SetCellValue(res[i].AreaName + "/" + res[i].RackIdName + "/" + res[i].LocationName);
                rt.CreateCell(1).SetCellValue(res[i].Code);
                rt.CreateCell(2).SetCellValue(res[i].Name);
                rt.CreateCell(3).SetCellValue(res[i].Spce);
                rt.CreateCell(4).SetCellValue(res[i].StorageName);
                rt.CreateCell(5).SetCellValue(res[i].storageIdName);
                rt.CreateCell(6).SetCellValue(Convert.ToDouble(res[i].Inventoryquantity.ToString()));
                rt.CreateCell(7).SetCellValue(Convert.ToDouble(res[i].Weight.ToString()));
                rt.CreateCell(8).SetCellValue(Convert.ToDouble(res[i].TotalWeight.ToString()));
                rt.CreateCell(9).SetCellValue(Convert.ToDouble(res[i].Availablequantity.ToString()));
                rt.CreateCell(10).SetCellValue(Convert.ToDouble(res[i].Nums_R.ToString()));
            }
            //输出的文件名称
            string fileName = "库存报表" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff") + ".xls";
            //把Excel转为流，输出
            //创建文件流
            System.IO.MemoryStream bookStream = new System.IO.MemoryStream();
            //将工作薄写入文件流
            book.Write(bookStream);

            //输出之前调用Seek（偏移量，游标位置) 把0位置指定为开始位置
            bookStream.Seek(0, System.IO.SeekOrigin.Begin);
            //Stream对象,文件类型,文件名称
            return File(bookStream, "application/vnd.ms-excel", fileName);
        }
        #endregion
        /// <summary>
        /// 总资产统计
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResultData<IT_TotalAssetsDto>> GetTotalAssetsList(PageInput<IT_LocalDetailDtolistContionDTO> input) {
            var res = await _iT_LocalMaterialBus.GetTotalAssetsList(input);
            return res;
        }
        /// <summary>
        /// 项目资产统计
        /// </summary>
        /// <param name="MaterialName"></param>
        /// <returns></returns>
        public async Task<IActionResult> GetProjectTotalAssetsExport(string MaterialName) {
            PageInput<IT_LocalDetailDtolistContionDTO> input = new PageInput<IT_LocalDetailDtolistContionDTO>();
            input.Search.MaterialName = MaterialName;
            var res = await _iT_LocalMaterialBus.ProjectAssetsList(input);
            HSSFWorkbook excelBook = new HSSFWorkbook();
            ISheet sheet1 = excelBook.CreateSheet("项目资产统计");
            IRow row0Temp = sheet1.CreateRow(0);
            row0Temp.CreateCell(0).SetCellValue("项目名称");
            row0Temp.CreateCell(1).SetCellValue("购销单位");
            row0Temp.CreateCell(2).SetCellValue("机位数量");
            row0Temp.CreateCell(3).SetCellValue("重量(T)");
            row0Temp.CreateCell(4).SetCellValue("总件数");
            for (int i=0;i<res.Data.Count;i++) {
                IRow row2Temp = sheet1.CreateRow(i+1);
                row2Temp.CreateCell(0).SetCellValue(res.Data[i].Name);
                row2Temp.CreateCell(1).SetCellValue(res.Data[i].PurchasingSelling);
                row2Temp.CreateCell(2).SetCellValue(Convert.ToDouble(res.Data[i].StandsNums));
                row2Temp.CreateCell(3).SetCellValue(Convert.ToDouble(res.Data[i].ShippedWeight));
                row2Temp.CreateCell(4).SetCellValue(Convert.ToDouble(res.Data[i].ShippedNums));
            }
            string fileName = "项目资产统计" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff") + ".xls";
            System.IO.MemoryStream bookStream = new System.IO.MemoryStream();
            excelBook.Write(bookStream);
            bookStream.Seek(0, System.IO.SeekOrigin.Begin);
            return File(bookStream, "application/vnd.ms-excel", fileName);
        }


        [NoCheckJWT]
        //总资产统计
        [HttpGet]
        public async Task<IActionResult> GetTotalAssetsExport(string MaterialName) {
            PageInput<IT_LocalDetailDtolistContionDTO> input = new PageInput<IT_LocalDetailDtolistContionDTO>();
            input.Search.MaterialName = MaterialName;
            var res = await _iT_LocalMaterialBus.GetTotalAssetsList(input);
            HSSFWorkbook excelBook = new HSSFWorkbook();
            ISheet sheet1 = excelBook.CreateSheet("总资产统计");
            sheet1.AddMergedRegion(new CellRangeAddress(0, 1, 0, 0));//
            sheet1.AddMergedRegion(new CellRangeAddress(0, 1, 1, 1));//
            sheet1.AddMergedRegion(new CellRangeAddress(0, 1, 2, 2));//
            sheet1.AddMergedRegion(new CellRangeAddress(0, 0, 3, 4));//
            sheet1.AddMergedRegion(new CellRangeAddress(0, 0, 5, 6));//
            sheet1.AddMergedRegion(new CellRangeAddress(0, 0, 7, 8));//
            sheet1.AddMergedRegion(new CellRangeAddress(0, 0, 9, 10));//
            IRow row0Temp = sheet1.CreateRow(0);
            row0Temp.CreateCell(0).SetCellValue("成品编码");
            row0Temp.CreateCell(1).SetCellValue("物料名称");
            row0Temp.CreateCell(2).SetCellValue("物料规格");
            row0Temp.CreateCell(3).SetCellValue("项目");
            row0Temp.CreateCell(5).SetCellValue("在库");
            row0Temp.CreateCell(7).SetCellValue("在途");
            row0Temp.CreateCell(9).SetCellValue("总计");

            IRow row0Temp2 = sheet1.CreateRow(1);
            row0Temp2.CreateCell(3).SetCellValue("数量");
            row0Temp2.CreateCell(4).SetCellValue("重量");
            row0Temp2.CreateCell(5).SetCellValue("数量");
            row0Temp2.CreateCell(6).SetCellValue("重量");
            row0Temp2.CreateCell(7).SetCellValue("数量");
            row0Temp2.CreateCell(8).SetCellValue("重量");
            row0Temp2.CreateCell(9).SetCellValue("数量");
            row0Temp2.CreateCell(10).SetCellValue("重量");



            for (int i=0;i< res.Data.Count;i++) {
                IRow row = sheet1.CreateRow(i+2);
                row.CreateCell(0).SetCellValue(res.Data[i].ProductCode);
                row.CreateCell(1).SetCellValue(res.Data[i].MaterialName);
                row.CreateCell(2).SetCellValue(res.Data[i].Spec);
                row.CreateCell(3).SetCellValue(Convert.ToDouble(res.Data[i].MaterialOfNums));
                row.CreateCell(4).SetCellValue(Convert.ToDouble(res.Data[i].MaterialOfWeight));
                row.CreateCell(5).SetCellValue(Convert.ToDouble(res.Data[i].MaterialwarehouseNums));
                row.CreateCell(6).SetCellValue(Convert.ToDouble(res.Data[i].MaterialwarehouseWeight));
                row.CreateCell(7).SetCellValue(Convert.ToDouble(res.Data[i].MaterialtransitNums));
                row.CreateCell(8).SetCellValue(Convert.ToDouble(res.Data[i].MaterialtransitWeight));
                row.CreateCell(9).SetCellValue(Convert.ToDouble(res.Data[i].MaterialOfNums)+Convert.ToDouble(res.Data[i].MaterialwarehouseNums)+Convert.ToDouble(res.Data[i].MaterialtransitNums));
                row.CreateCell(10).SetCellValue(Convert.ToDouble(res.Data[i].MaterialOfWeight) + Convert.ToDouble(res.Data[i].MaterialwarehouseWeight) + Convert.ToDouble(res.Data[i].MaterialtransitWeight));

            }

            //输出的文件名称
            string fileName = "总资产统计" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff") + ".xls";
            //把Excel转为流，输出
            //创建文件流
            System.IO.MemoryStream bookStream = new System.IO.MemoryStream();
            //将工作薄写入文件流
            excelBook.Write(bookStream);

            //输出之前调用Seek（偏移量，游标位置) 把0位置指定为开始位置
            bookStream.Seek(0, System.IO.SeekOrigin.Begin);
            //Stream对象,文件类型,文件名称
            return File(bookStream, "application/vnd.ms-excel", fileName);
        }
   

            #region   库存汇总(每月结存)

            /// <summary>
            /// 期初库存结存
            /// </summary>
            /// <returns></returns>
            [HttpPost]
        public async Task GetStartInventoryBalance()
        {
            await _iT_LocalMaterialBus.GetStartInventoryBalanceAsync();
        }

        /// <summary>
        /// 上月结存
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task GetLastMonthBalance()
        {
            await _iT_LocalMaterialBus.GetLastMonthBalanceAsync();
        }

        /// <summary>
        /// 获取库存结存信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<MaterialBalance>> GetMaterialBalanceList(PageInput<MaterialBalanceConditionDTO> input)
        {
            return await _iT_LocalMaterialBus.GetMaterialBalanceListAsync(input);
        }

        /// <summary>
        /// 库存结存信息导出
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [NoCheckJWT]
        public async Task<IActionResult> GetExportMaterialBalanceList(MaterialBalanceConditionDTO input)
        {
         
            List<MaterialBalance> listdata = await _iT_LocalMaterialBus.GetMaterialBalanceExcelAsync(input);
            HSSFWorkbook book = new HSSFWorkbook();//设置指定xl格式为xls
            ISheet s1 = book.CreateSheet("收发存表");
            IRow r1 = s1.CreateRow(0);
            r1.CreateCell(0).SetCellValue("物料Id");
            r1.CreateCell(1).SetCellValue("仓库名称");
            r1.CreateCell(2).SetCellValue("物料名称");
            r1.CreateCell(3).SetCellValue("物料规格");
            r1.CreateCell(4).SetCellValue("期初数量");
            r1.CreateCell(5).SetCellValue("入库数量");
            r1.CreateCell(6).SetCellValue("出库数量");
            r1.CreateCell(7).SetCellValue("结存数量");

            for (int i = 0; i < listdata.Count; i++)
            {
                IRow rowData = s1.CreateRow(i + 1);
                ICell cel = rowData.CreateCell(0);
                cel.SetCellValue(listdata[i].MaterialId.ToString());

                cel = rowData.CreateCell(1);
                cel.SetCellValue(listdata[i].StorName == null ? "" : listdata[i].StorName.ToString());

                cel = rowData.CreateCell(2);
                cel.SetCellValue(listdata[i].MaterialName.ToString());

                cel = rowData.CreateCell(3);
                cel.SetCellValue(listdata[i].MaterialSpec == null ? "" : listdata[i].MaterialSpec.ToString());

                cel = rowData.CreateCell(4);
                cel.SetCellValue(Convert.ToDouble(listdata[i].LastMonth.ToString()));

                cel = rowData.CreateCell(5);
                cel.SetCellValue(Convert.ToDouble(listdata[i].InNums.ToString()));

                cel = rowData.CreateCell(6);
                cel.SetCellValue(Convert.ToDouble(listdata[i].OutNums.ToString()));

                cel = rowData.CreateCell(7);
                cel.SetCellValue(Convert.ToDouble(listdata[i].ThisNums.ToString()));
            }
            string fileName = "收发存表" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff") + ".xls";
            System.IO.MemoryStream bookStream = new System.IO.MemoryStream();
            book.Write(bookStream);
            bookStream.Seek(0, System.IO.SeekOrigin.Begin);
            return File(bookStream, "application/vnd.ms-excel", fileName);
        }

        /// <summary>
        /// 获取结存年份
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<string>> GetYear()
        {
            return await _iT_LocalMaterialBus.GetYearAsync();
        }

        /// <summary>
        /// 获取年月下的月分结存信息
        /// </summary>
        /// <param name="year">年份</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<YearMonthDTO>> GetYearMonthBalance(string year)
        {
            return await _iT_LocalMaterialBus.GetYearMonthBalanceAsync(year);
        }

        /// <summary>
        /// 获取结存详情
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<MaterialBalance>> GetYearMonthBalanceListAsync(PageInput<BalanceConditionDTO> input)
        {
            return await _iT_LocalMaterialBus.GetYearMonthBalanceListAsync(input);
        }

        /// <summary>
        /// 结存回滚
        /// </summary>
        /// <param name="yearmonth">年月</param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpRollBACK(string yearmonth)
        {
            await _iT_LocalMaterialBus.UpRollBACKAsync(yearmonth);
        }

        /// <summary>
        /// 本月结存
        /// </summary>
        /// <param name="yearmonth">年月</param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpBalance(string yearmonth)
        {
            await _iT_LocalMaterialBus.UpBalanceAsync(yearmonth);
        }
        /// <summary>
        /// 库龄分析导出
        /// </summary>
        /// <returns></returns>
        /// 

        public static string hddd(ICell cell){
            var ddd = string.Empty;
            var cellReference = string.Empty;
            if (cell!=null) {
                cellReference = new CellReference(cell).FormatAsString();
            }
            return cellReference;
}
       
        [HttpGet]
        public async Task<IActionResult> GetExportStockAnalysis(Boolean state) {

            var GetBase_DateRangeList = await _iT_LocalMaterialBus.GetBase_DateRangeList();
            PageInput pageInput = new PageInput();
            //查询数据
            var listFaultInfo = await _iT_LocalMaterialBus.GetStorageCycleDataListAsync(pageInput);
            //二：代码创建一个Excel表格（这里称为工作簿）
            //创建Excel文件的对象 工作簿(调用NPOI文件)
            HSSFWorkbook excelBook = new HSSFWorkbook();
            ICellStyle style2 = excelBook.CreateCellStyle();
            ICellStyle styleKK = excelBook.CreateCellStyle();
            ICellStyle styleRight = excelBook.CreateCellStyle();
            ICellStyle style3 = excelBook.CreateCellStyle();
            ICellStyle style4 = excelBook.CreateCellStyle();
            IFont font = excelBook.CreateFont();
            font.Color = IndexedColors.Black.Index;
            style3.SetFont(font);
            style2.Alignment = HorizontalAlignment.Center;
            styleRight.Alignment = HorizontalAlignment.Right;
            style2.VerticalAlignment = VerticalAlignment.Center;
            style3.Alignment = HorizontalAlignment.Center;
            style3.VerticalAlignment = VerticalAlignment.Center;

            IDataFormat format = excelBook.CreateDataFormat();
            ICellStyle cStyle1 = excelBook.CreateCellStyle();

            cStyle1.DataFormat = format.GetFormat("_ * #,##0.00_ ;_ * -#,##0.00_ ;_ * \"-\"??_ ;_ @_");//单元格格式 数值

            //创建Excel工作表 Sheet=故障码信息
            //标题
            ISheet sheet1 = excelBook.CreateSheet("库龄分析");

    
         
            //单元格合并 
            //sheet1.AddMergedRegion(new CellRangeAddress(0, 0, 0, 7));//合并单元格,4个参数依次为为startRow,endRow,startCol,endCo

            //设计清单名称
            IRow row0Temp = sheet1.CreateRow(0);
       
            row0Temp.CreateCell(0).SetCellValue("编号");
            row0Temp.GetCell(0).CellStyle = style2;//初始化样式
            row0Temp.Height = 400;
            row0Temp.CreateCell(1).SetCellValue("名称");
            row0Temp.GetCell(1).CellStyle = style2;//初始化样式
            row0Temp.Height = 400;
            row0Temp.CreateCell(2).SetCellValue("规格");
            row0Temp.GetCell(2).CellStyle = style2;//初始化样式
            row0Temp.Height = 400;
            row0Temp.CreateCell(3).SetCellValue("理论重量");
            row0Temp.GetCell(3).CellStyle = style2;//初始化样式
            row0Temp.Height = 400;
            Dictionary<string, string> map = new Dictionary<string, string>();
            int _index = 0;
            for (int i=0;i< GetBase_DateRangeList.Count;i++) {
                row0Temp.CreateCell(i+4).SetCellValue(GetBase_DateRangeList[i].BeginDay.ToString()+"-"+GetBase_DateRangeList[i].EndDay.ToString()+"天");
                row0Temp.GetCell(i+4).CellStyle = style2;//初始化样式
                row0Temp.Height = 400;
                _index=i;
              
            }
            int a = _index+5;
            row0Temp.CreateCell(a).SetCellValue("总计");
            row0Temp.GetCell(a).CellStyle = style2;//初始化样式
            row0Temp.Height = 400;
          
            string Number = "D";

            List<String> AA = new List<string>();
            if (state == true)
            {
                for (int i = 0; i < listFaultInfo.Data.Count; i++)
                {
                    IRow row1 = sheet1.CreateRow(i + 1);

                    row1.CreateCell(0).SetCellValue(listFaultInfo.Data[i].Code);
                    row1.CreateCell(1).SetCellValue(listFaultInfo.Data[i].Name);
                    row1.CreateCell(2).SetCellValue(listFaultInfo.Data[i].Spec);
                    row1.CreateCell(3).SetCellValue(Convert.ToDouble(listFaultInfo.Data[i].DiffDay));
                    System.Reflection.PropertyInfo[] properties = listFaultInfo.Data[i].GetType().GetProperties();
                    int index = 4;
                    for (int O = 0; O < GetBase_DateRangeList.Count(); O++)
                    {
                        String name = "D" + O;
                        foreach (var item in properties) {
                            
                                AA.Add(item.Name);
                        }
                        if (AA.Contains(name))
                        {
                            int indexs = AA.FindIndex(item => item.Equals(name));
                            string value = properties[indexs].GetValue(listFaultInfo.Data[i]).ToString();
                            row1.CreateCell(index).SetCellValue(Convert.ToDouble(value));
                            index += 1;
                        }
                        int SUM = GetBase_DateRangeList.Count + 3;
                       
                    }
                    row1.CreateCell(GetBase_DateRangeList.Count + 4).SetCellFormula("SUM(" + hddd(row1.GetCell(4)) + ":" + hddd(row1.GetCell(GetBase_DateRangeList.Count + 3)) + ")");
                }
            }
            else {
                for (int i = 0; i < listFaultInfo.Data.Count; i++)
                {
                    IRow row1 = sheet1.CreateRow(i + 1);

                    row1.CreateCell(0).SetCellValue(listFaultInfo.Data[i].Code);
                    row1.CreateCell(1).SetCellValue(listFaultInfo.Data[i].Name);
                    row1.CreateCell(2).SetCellValue(listFaultInfo.Data[i].Spec);
                    row1.CreateCell(3).SetCellValue(listFaultInfo.Data[i].DiffDay);
                    System.Reflection.PropertyInfo[] properties = listFaultInfo.Data[i].GetType().GetProperties();
                    int index = 4;
                    for (int O = 0; O < GetBase_DateRangeList.Count(); O++)
                    {
                        String name = "W" + O;
                        foreach (var item in properties)
                        {

                            AA.Add(item.Name);
                        }
                        if (AA.Contains(name))
                        {
                            int indexs = AA.FindIndex(item => item.Equals(name));
                            string value = properties[indexs].GetValue(listFaultInfo.Data[i]).ToString();
                            row1.CreateCell(index).SetCellValue(Convert.ToDouble(value));
                            row1.CreateCell(index).CellStyle = cStyle1;
                            index += 1;
                        }
                        int SUM = GetBase_DateRangeList.Count + 3;

                    }
                    row1.CreateCell(GetBase_DateRangeList.Count + 4).SetCellFormula("SUM(" + hddd(row1.GetCell(4)) + ":" + hddd(row1.GetCell(GetBase_DateRangeList.Count + 3)) + ")");
                }

            }
          
           

            //输出的文件名称
            string fileName = "清单导出" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff") + ".xls";
            //把Excel转为流，输出
            //创建文件流
            System.IO.MemoryStream bookStream = new System.IO.MemoryStream();
            //将工作薄写入文件流
            excelBook.Write(bookStream);

            //输出之前调用Seek（偏移量，游标位置) 把0位置指定为开始位置
            bookStream.Seek(0, System.IO.SeekOrigin.Begin);
            //Stream对象,文件类型,文件名称
            return File(bookStream, "application/vnd.ms-excel", fileName);

        }
        #endregion

        #region 提交

        [HttpPost]
        public async Task SaveData(IT_LocalMaterial data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);

                await _iT_LocalMaterialBus.AddDataAsync(data);
            }
            else
            {
                await _iT_LocalMaterialBus.UpdateDataAsync(data);
            }
        }
        [HttpPost]
        public async Task<PageResultData<IT_ProjectLosses>> IGetProjectLosses(PageInput<IT_LocalDetailDtolistContionDTO> input) {
            var res = await _iT_LocalMaterialBus.IGetProjectLosses(input);
            return res;
        }

        [HttpPost]
        public async Task<PageResultData<PB_ProjectAssets>> ProjectAssetsList(PageInput<IT_LocalDetailDtolistContionDTO> input) {
            var res =await _iT_LocalMaterialBus.ProjectAssetsList(input);
            return res;
        }
        [HttpPost]
        public async Task DeleteData(List<string> ids)
        {
            await _iT_LocalMaterialBus.DeleteDataAsync(ids);
        }

        #endregion

        #region 重置占用量

        /// <summary>
        /// 重置占用量
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task ResettingUsage()
        {
            await _iT_LocalMaterialBus.ResettingUsageAsync();
        }

        #endregion

    }
}